package com.bear.cmcc.NetTestHTTP.ui.chart;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.View;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 测试工具波形图
 * 
 * @author zhangheng
 * 
 */
public class TestToolChartView extends AbstractDemoChart {
	/** The main dataset that includes all the series that go into a chart. */
	private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
	/** The main renderer that includes all the renderers customizing a chart. */
	private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
	/** The most recently added series. */
	private XYSeries mCurrentSeries;

	private int mCount;

	private GraphicalView mGraphicalView;

	/**
	 * Returns the chart name.
	 * 
	 * @return the chart name
	 */
	public String getName() {
		return "Speed Wav Chart";
	}

	/**
	 * Returns the chart description.
	 * 
	 * @return the chart description
	 */
	public String getDesc() {
		return "Net Test Spend Wav";
	}
	
	public void ClearData()
	{
		mCurrentSeries.clear();
		mGraphicalView.repaint();
	}

	public void updateChartView(double aveValue, double maxValue) {
		mCount++;
		mCurrentSeries.add(mCount, aveValue);
		
		int startX = 0;
		if(mCount-60>0)
		{
			startX = mCount-60;
		}
		setChartSettings(mRenderer, "", "", "", startX, startX+60, 0, maxValue, Color.LTGRAY, Color.BLACK);

		if(mCount<60)
		{
			if(mCount%10 == 0)
			{
				mRenderer.addXTextLabel(mCount, String.valueOf(mCount)+"s");
			}
			else
			{
//				mRenderer.addXTextLabel(mCount, "");
			}		
			mRenderer.setPanLimits(new double[] { 0, 60, 0,
					maxValue });// HINT 限制XY轴起始范围
		}
		else
		{
			if(mCount % 60 == 0)
			{
				mRenderer.addXTextLabel(mCount, String.valueOf(mCount/60)+"m");
			}
			else
			{
//				mRenderer.addXTextLabel(mCount, "");
			}
			mRenderer.setPanLimits(new double[] { 0, mCount, 0,
					maxValue });// HINT 限制XY轴起始范围
		}
		
//		XYMultipleSeriesDataset dataset = buildDataset(titles, datas);

		// 设置横轴适应的lable数量
//		 mRenderer.setXLabels(2);
		// 设置纵轴适应的lable数量
		// mRenderer.szetYLabels(2);
		mGraphicalView.repaint();
		 
	}

	/**
	 * Executes the chart demo.
	 * 
	 * @param context
	 *            the context
	 * @return the built intent
	 */
	public View obtainChartView(Context context) {

		XYSeries series = new XYSeries("");
		mDataset.addSeries(series);
		mCurrentSeries = series;

		int[] colors = new int[] { Color.rgb(3, 108, 140) };
		PointStyle[] styles = new PointStyle[] { PointStyle.POINT };
		mRenderer = buildRenderer(colors, styles);
		// 限制XY轴起始范围
		// mRenderer.setPanLimits(new double[] { 0, 10000, 0, 100000 });
		// 限制放大的XY起始范围
		mRenderer.setZoomLimits(new double[] { 1, 1, 1, 1 });
		// 设置是否显示横纵轴标签
		mRenderer.setShowLabels(true);
		// 设置是否显示横轴标签
		mRenderer.setXShowLabels(true);
		// 设置是否显示纵轴标签
		mRenderer.setYShowLabels(false);

		XYSeriesRenderer seriesRenderer = (XYSeriesRenderer) mRenderer
				.getSeriesRendererAt(0);
		seriesRenderer.setFillBelowLine(true);
		seriesRenderer.setFillBelowLineColor(colors[0]);
		seriesRenderer.setLineWidth(2.5f);
		seriesRenderer.setDisplayChartValues(false);
		seriesRenderer.setChartValuesTextSize(10f);

		mGraphicalView = ChartFactory.getCubeLineChartView(context, mDataset,
				mRenderer, 0.5f);

		return mGraphicalView;
	}

}
